<?php


namespace app\common\model\supplier;

use app\common\model\BaseModel;
use app\common\model\order\Order;
use app\common\model\supplier\InvoiceLog;

/**
 * 供应商服务保障模型
 */
class InvoiceOrder extends BaseModel
{
    protected $pk = 'id';
    protected $name = 'supplier_invoice_order';

    public function invoice()
    {
        return $this->hasOne(Invoice::class, 'id', 'invoice_id');
    }

    public function order()
    {
        return $this->hasOne(Order::class, 'order_id', 'order_id');
    }

    public function getStatusAttr($value)
    {
        $array = ['开票中','已开票'];
        return ['text' => $array[$value]??'开票中', 'value' => $value];
    }
    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $model = self::assWhere($model, $where);
        $list = $model->paginate($params);
        return $list;
    }

    public static function createData($where)
    {
        $model = new static();
        $where['app_id'] = self::$app_id;
        $data = $model->save($where);

        $invoice_log['invoice_id'] = $where['invoice_id'];
        $invoice_log['order_id'] = $where['order_id'];
        $invoice_log['uid'] = $where['uid'];
        $invoice_log['status'] = 0;
        InvoiceLog::createData($invoice_log);
        return $data;
    }

    public static function one($where, $with = [])
    {
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        return $model->findOrEmpty($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = self::assWhere($model, $where);
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $list = $model->select();
        return $list;
    }
}
